<template>
  <div class="register">
    <el-form
      ref="registerFormRef"
      :model="registerForm"
      :rules="registerRules"
      class="register-form"
      @keyup.enter="handleKeyPress"
    >
      <h3 class="title">注册</h3>
      <el-form-item prop="username">
        <el-input
          v-model="registerForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleRegister"
        >
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="standing">
        <el-select v-model="registerForm.standing" placeholder="请选择身份">
          <template #prefix>
            <el-icon><Avatar /></el-icon>
          </template>
          <el-option label="老师" value="0"></el-option>
          <el-option label="学生" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中 ...</span>
        </el-button>
      </el-form-item>
      <el-form-item style="text-align: center; margin-top: 10px">
        <router-link to="/" class="login-link">已有账号?去登陆</router-link>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2024 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  ElForm,
  ElFormItem,
  ElInput,
  ElButton,
  ElMessage,
  ElSelect,
  ElOption,
} from "element-plus";
import { register } from "@/api/login.js"; // 确保路径正确
import { useUserStore } from "@/stores"; // 确保路径正确

const route = useRoute();
const router = useRouter();
const userStore = useUserStore(); // 使用用户存储
const registerForm = reactive({
  username: "",
  password: "",
  standing: "",
});
const registerRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  standing: [{ required: true, message: "请选择身份", trigger: "change" }],
};
const loading = ref(false);
const registerFormRef = ref(null);

const handleRegister = async () => {
  if (registerFormRef.value) {
    const valid = await registerFormRef.value.validate();
    if (valid) {
      loading.value = true;
      try {
        const response = await register(registerForm);
        if (response.data.code === 200) {
          ElMessage.success("注册成功");
          // 注册成功后跳转到登录页面
          router.push("/");
        } else {
          ElMessage.error("注册失败：" + response.data.message);
        }
      } catch (error) {
        ElMessage.error("注册失败：" + error.message);
      } finally {
        loading.value = false;
      }
    }
  }
};

// 监听回车键进行注册
const handleKeyPress = (event) => {
  if (event.key === "Enter") {
    handleRegister();
  }
};

onMounted(() => {});
</script>
<style scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url("@/assets/images/loginbg.jpg");
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}

.register-code {
  display: flex;
  align-items: center;
  float: right;
}
.register-code-img {
  cursor: pointer;
  height: 38px;
}
.login-link {
  color: #409eff;
  text-decoration: none;
  margin-top: 20px;
  display: block;
}
</style>
